package com.xique.door.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.constant.Constants;
import com.xique.common.core.utils.StringUtils;
import com.xique.door.bean.domain.WxMessageTemplate;
import com.xique.door.bean.search.WxMessageTemplateListSearch;
import com.xique.door.mapper.WxMessageTemplateMapper;
import com.xique.door.service.IWxMessageTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author shd
 * @create 2022/4/26 17:09
 */
@Service
public class WxMessageTemplateServiceImpl extends ServiceImpl<WxMessageTemplateMapper, WxMessageTemplate> implements IWxMessageTemplateService {

    @Autowired
    private WxMessageTemplateMapper templateMapper;

    @Override
    public List<WxMessageTemplate> selectTemplateList(WxMessageTemplateListSearch search) {
        QueryWrapper<WxMessageTemplate> condition = new QueryWrapper<>();
        if (StringUtils.isNotNull(search.getType())) {
            condition.eq("type", search.getType());
        }
        if (StringUtils.isNotEmpty(search.getStatus())) {
            condition.eq("status", search.getStatus());
        }
        condition.le("start_time", new Date()).ge("end_time", new Date());
        condition.orderByDesc("update_time");

        return templateMapper.selectList(condition);
    }

    @Override
    public IPage<WxMessageTemplate> selectTempLatePage(WxMessageTemplateListSearch search) {
        Page<WxMessageTemplate> page = new Page<>();
        if (StringUtils.isNotNull(search.getPageNum()) && StringUtils.isNotNull(search.getPageSize())) {
            page.setCurrent(search.getPageNum());
            page.setSize(search.getPageSize());
        }
        return templateMapper.selectTemplatePage(page, search);
    }

    @Override
    public int checkTemplateUnique(Integer type) {
        if (StringUtils.isNull(type)) {
            return 1;
        }
        QueryWrapper<WxMessageTemplate> condition = new QueryWrapper<>();
        condition.eq("type", type)
        .eq("status", Constants.NORMAL);
        return templateMapper.selectCount(condition);
    }
}
